<template>
  <section class="paging-box" v-cloak v-if="totalSize > 0">
    <ul class="paging-list">
      <li class="paging-item number-item" :class="{disabled: curPage === 1}" @click="switchPage(1)">首页</li>
      <li class="paging-item" :class="{disabled: curPage === 1}" @click="switchPage(curPage-1)">上一页</li>
      <li
        class="paging-item number-item"
        v-if="indexArr && indexArr.length > 0"
        v-for="item in indexArr"
        :key="item"
        :class="{active: curPage === item}"
         @click="switchPage(item)"
      >{{item}}</li>
      <li class="paging-item" :class="{disabled: curPage === total}" @click="switchPage(curPage+1)">下一页</li>
      <li class="paging-item number-item" :class="{disabled: curPage === total}" @click="switchPage(total)">尾页</li>
    </ul>
  </section>
</template>

<script>
export default {
  name: 'Paging',

  props: {
    cur: {
      type: Number,
      default: 1
    },
    size: {
      type: Number,
      default: 0
    },
    visibleNum: {
      type: Number,
      default: 5
    },
    pageSizes: {
      type: Array,
      default: function () {
        return [10, 20, 30, 50]
      }
    },
    pageSize: {
      type: Number,
      default: 20
    }
  },

  data () {
    return {
      curPage: this.cur,
      visible: this.visibleNum,
      // pageSize: this.pageSize,
      totalSize: this.size
    }
  },

  methods: {
    switchPage (index) {
      if (this.curPage !== index) {
        this.curPage = index
        this.$emit('update:cur', index)
        this.$emit('backData')
      }
    }
  },

  computed: {
    total () {
      return Math.ceil(this.totalSize / this.pageSize)
    },
    indexArr () {
      let left = 1,
          visible = this.visible,
          total = this.total,
          right = total,
          curPage = this.curPage,
          half = Math.ceil(visible / 2),
          resArr = [];
      if (total > visible) {
        if (curPage < half) {
          left = 1
          right = visible
        } else if (curPage >= total - half + 1) {
          right = total
          left = total - visible + 1
        } else {
          left = curPage - half + 1
          right = left + visible - 1
        }
      }

      while (left <= right) {
        resArr.push(left)
        left ++
      }

      return resArr
    }
  },

  watch: {
    cur (Val) {
      if (this.curPage !== Val) {
        this.cur = Val
      }
    },

    size (Val) {
      this.totalSize = Val
    }
  }
}
</script>

<style lang="less" scoped>
@greydd: #DDD;
@blue: #5995D8;
.paging-list {
  display: flex;
  align-items: center;
  height: 30px;
  line-height: 30px;
}
.paging-item {
  width: 60px;
  text-align: center;
  border: 1px solid @greydd;
  user-select: none;
  cursor: pointer;

  &.disabled {
    pointer-events: none;
    cursor: not-allowed;
  }

  &.active, &:not(.disabled):hover {
    border-color: @blue;
    background-color: @blue;
    color: #FFF;
  }

  &.number-item {
    width: 50px;
  }

  + .paging-item {
    border-left: 0;
  }

  &:first-child {
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
  }

  &:last-child {
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
  }
}
</style>
